package admin

import (
	"github.com/gin-gonic/gin"
	"ylm.sl/global"
	"ylm.sl/internal/api/controller"
	"ylm.sl/internal/repository/dao"
	"ylm.sl/internal/service"
	"ylm.sl/middle"
)

type UserRouter struct {
	service service.IUserService
}

func (ur *UserRouter) InitApiRouter(router *gin.RouterGroup) {
	ur.service = service.NewUserService(dao.NewUserDao(global.DB),
		(*global.Bloom)["userRegisterCachePenetrationBloomFilter"])
	userController := controller.NewUserController(ur.service)
	publicRouter := router.Group("user")
	{
		// 注册用户
		publicRouter.POST("", userController.Register)
		// 用户登录
		publicRouter.POST("login", userController.Login)
	}

	privateRouter := router.Group("user")
	privateRouter.Use(middle.VerifyToken())
	{
		//查询用户名是否存在
		privateRouter.GET("/has-username", userController.HasUsername)
		// 跟据用户名查找用户
		privateRouterActual := router.Group("actual")
		privateRouterActual.Use(middle.VerifyToken())
		privateRouterActual.GET("/user/:username", userController.GetActualUserByUsername)
		// 修改用户
		privateRouter.PUT("", userController.Update)
		// 检查用户是否登录
		privateRouter.GET("/check-login", userController.CheckLogin)
		// 用户退出登录
		privateRouter.DELETE("/logout", userController.Logout)
	}
}
